/***************************************************************************
 *   Copyright (C) 2007 by Ivan Vasi?                                      *
 *   ivasic@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "torrentpropertiesdialog.h"
#include "modeltorrent.h"
#include "globals.h"
#include "log.h"
#include "trackerdb.h"
#include "exceptions.h"
#include "exceptionhandler.h"
#include "functions.h"

#include <QtCore>
#include <QtGui>

TorrentPropertiesDialog::TorrentPropertiesDialog(ModelTorrent* torrent, QWidget* parent, Qt::WFlags fl)
		: QDialog(parent, fl), m_torrent(torrent)
{
	setupUi(this);

	m_filesModel = m_torrent->getFilesModel();

	fillForm();
	
	connect(btnUpdateDescription, SIGNAL(clicked()), this, SLOT(updateDescription()));
}

TorrentPropertiesDialog::~TorrentPropertiesDialog()
{
	delete m_filesModel;
}

/*$SPECIALIZATION$*/

void TorrentPropertiesDialog::fillFilesTree()
{
	if (m_filesModel->item(0)->hasChildren())
	{
		m_filesModel->item(0)->setIcon(QIcon(":/images/icons/folder.png"));
		setTreeNodeIcon(m_filesModel->item(0));
	}
	else
		m_filesModel->item(0)->setIcon(QIcon(":/images/icons/file.png"));

	//QTreeView* treeFiles;
	treeFiles->setModel(m_filesModel);
}

void TorrentPropertiesDialog::setTreeNodeIcon(QStandardItem* parent)
{
	QString trt = parent->text();

	if (parent->hasChildren())
		parent->setIcon(QIcon(":/images/icons/folder.png"));
	else
		parent->setIcon(QIcon(":/images/icons/file.png"));

	for (int i = 0; i < parent->rowCount(); ++i)	
		setTreeNodeIcon(parent->child(i));	
}


void TorrentPropertiesDialog::fillForm()
{
	lblName->setText(m_torrent->name());
	lblInfohash->setText(tr("Infohash: ") + m_torrent->infohash());
	lblSize->setText(tr("Size: %1").arg(bytesToString(m_torrent->size())));
	lblUploadedBy->setText(tr("Uploaded by: ") + m_torrent->uploadedBy());
	lblAdded->setText(tr("Date added: ") + m_torrent->added().toString("dd/mm/yyyy"));
	
	txtDescription->setText(m_torrent->description());	
	
	fillFilesTree();
}

void TorrentPropertiesDialog::updateDescription()
{
	try
	{
		Globals::instance().db()->updateTorrentDescription(m_torrent->infohash(), txtDescription->toPlainText());
		
		QMessageBox::information(this, tr("Success"), tr("Description updated"));
	}
	catch (Exception* ex)
	{
		ExceptionHandler::instance().protocolException(this->thread(), ex, TRUE);
	}
}
